package com.qianchang.microfinance.view.mine.intention;

import android.os.Bundle;

import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.LoanIntentDetailBean;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.utils.GetJsonDataUtil;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.element.DTextView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 借款意向详情
 */
public class LoanIntentDetailActivity extends BaseActivity {

    private static final String TAG = "LoanIntentDetailActivity";

    @BindView(R.id.tv_borrow_money)
    DTextView tvBorrowMoney;
    @BindView(R.id.tv_loan_deadline)
    DTextView tvLoanDeadline;
    @BindView(R.id.tv_monthly_interest_rate)
    DTextView tvMonthlyInterestRate;
    @BindView(R.id.tv_repay_mode)
    DTextView tvRepayMode;
    @BindView(R.id.tv_mortgage_type)
    DTextView tvMortgageType;
    @BindView(R.id.tv_belong_city)
    DTextView tvBelongCity;
    @BindView(R.id.tv_examine_status)
    DTextView tvExamineStatus;
    @BindView(R.id.tv_apply_date)
    DTextView tvApplyDate;
    private String projectId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loan_intent_detail);
        ButterKnife.bind(this);
        initView();
        initData();
    }


    @Override
    protected void initView() {
        super.initView();
        setTitleName(getString(R.string.title_loan_intention_detail));
    }


    @Override
    protected void initData() {
        super.initData();
        Bundle bundle = getBundle();
        if (null != bundle) {
            projectId = bundle.getString(ConstantAppInfo.PROJECT_ID);
        }
        getIntentDetail();
    }

    private void getIntentDetail() {
        HttpParams params = new HttpParams();
        params.put("intentId", projectId);
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.LOAN_INTENT_DETAIL, params, new RequestCallBack<LoanIntentDetailBean>() {

            @Override
            public void onSuccess(JsonResult<LoanIntentDetailBean> response) {
                if (response.isReqDataSuccess()) {
                    LoanIntentDetailBean loanIntentDetailBean = response.getObject();
                    if (null != loanIntentDetailBean) {
                        refreshUi(loanIntentDetailBean);
                    }
                } else {
                    ToastUtil.getInstant().show(LoanIntentDetailActivity.this, response.getDescription());
                }
            }


            @Override
            public void onFailure(String msg) {
                ToastUtil.getInstant().show(LoanIntentDetailActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }

    private void refreshUi(LoanIntentDetailBean loanIntentDetailBean) {
        tvBorrowMoney.setTextContent(loanIntentDetailBean.getApplyAmount());
        tvLoanDeadline.setTextContent(loanIntentDetailBean.getDeadline() + "个月");
        tvMonthlyInterestRate.setTextContent(loanIntentDetailBean.getServiceFee() + "%");
        tvRepayMode.setTextContent(loanIntentDetailBean.getRepaymentType());
        tvMortgageType.setTextContent(loanIntentDetailBean.getCollateralName());
        tvBelongCity.setTextContent(GetJsonDataUtil.newInstance().getPrinceAndCityById(loanIntentDetailBean.getProvinceId(), loanIntentDetailBean.getCityId()) + GetJsonDataUtil.newInstance().getAreaById(loanIntentDetailBean.getCountyId()));
        tvExamineStatus.setTextContent(loanIntentDetailBean.getStatusDesc());
        String createTime = loanIntentDetailBean.getCreateTime();
        if (null != createTime && createTime.length() > 10) {
            createTime = createTime.substring(0, 10);
        }
        tvApplyDate.setTextContent(createTime);
    }
}
